package cn.jbolt.admin.xd.utils;

import cn.jbolt.common.model.xd.XdScmProduct;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.math.BigDecimal;
import java.util.List;

public class ProductUtils {
    public static void main(String[] args) {
        XdDBUtils.contect();
        step2();
    }

    private static void productImport(){
        try {
            String path = "/Users/weizixiong/Downloads/商品信息-20250712224820199.xlsx";
            FileInputStream fileInputStream = new FileInputStream(path);

            XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
            Sheet sheet = workbook.getSheetAt(0);
            for (int i = 3; i < sheet.getPhysicalNumberOfRows(); i++) {
                //获取第一行
                Row row = sheet.getRow(i);

                String title = row.getCell(2).getStringCellValue();
                String spec =  row.getCell(3).getStringCellValue();
                String unit = row.getCell(9).getStringCellValue();
                String rate = row.getCell(10).getStringCellValue();

                System.out.println(i+"==="+title+"==="+spec+"==="+unit+"==="+rate);

                XdScmProduct product = new XdScmProduct().dao().findFirst("select * from xd_scm_product where title=?",title);
                if (product == null) {
                    product = new XdScmProduct();
                    product.setIsDeleted(false);
                }
                product.setTitle(title);
                if (title.contains("]")){
                    String[] titleArr = title.split("]");
                    String brand = titleArr[0].replace("[","");
                    String name = titleArr[1];
                    product.setBrand(brand);
                    product.setName(name);
                }else{
                    product.setName(title);
                }
                product.setSpec(spec);
                product.setWeight(spec);
                if (unit.contains("/")){
                    String[] unitArr = unit.split("/");
                    String unit1 = unitArr[0];
                    String unit2 = unitArr[1];
//                    product.setMainUnit(unit2);
//                    product.setAuxiliaryUnit(unit1);

                    product.setMainUnit(unit1);
                    product.setAuxiliaryUnit(unit2);
                }else{
                    product.setMainUnit(unit);
                }
                if (rate.contains("/")){
                    String[] rateArr = rate.split("/");
//                    String rate1 = rateArr[1];
//                    product.setRatioForward(new BigDecimal(rate1));

                    String rate1 = rateArr[0];
                    product.setRatioForward(new BigDecimal(rate1));
                }

                if (product.getId() == null){
                    product.save();
                }else{
                    product.update();
                }
                System.out.println(product);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static  void step2(){
        List<XdScmProduct> productList = new XdScmProduct().dao().findAll();
        for (XdScmProduct product : productList){
            String untiDesc = product.getMainUnit();
            if (product.getRatioForward() != null){
                String result1 = product.getRatioForward().stripTrailingZeros().toPlainString();
                untiDesc = "1 " + product.getMainUnit() + "=" + result1+" " +product.getAuxiliaryUnit();
            }
            product.setUnitDesc(untiDesc);
            product.update();
        }
    }
}
